<template>
  <div class="search-header">
    <div class="types-row" ref="typesRow">
      <div class="search-types">
        <div
          class="item"
          v-for="(item,index) in showSearchTypes"
          :key="index"
          :class="{ active: item.active }"
          @click="changeSearchType(item,index)"
        >
          {{ item.text }}
        </div>
        <div class="more-item" v-if="searchTypesShowIndex<searchTypes.length-1" @click="moreVisible = !moreVisible">
          <span>更多</span>
          <img src="~@/assets/dashboard/down-arrow.png" width="11" height="7" alt="">
          <popup-selector
            :active-index="moreActiveIndex"
            :option-list="searchTypes.slice(searchTypesShowIndex)"
            :css-style="'top: 28px; left: 0;'"
            :visible="moreVisible"
            @clickOption="chooseMoreType"
          ></popup-selector>
        </div>
        <div class="item-divider"></div>
        <div class="options-item" @click="openManage">
          <img src="~@/assets/dashboard/options-item.png" width="11" height="11" alt="">
          <span>管理选项</span>
        </div>
      </div>
    </div>
    <div class="search-row">
      <el-input v-model="searchText" placeholder="请输入..."
                @focus="showHistory=true"
                @mouseenter.native="mouseOverInput=true" @mouseleave.native="mouseOverInput=false"
      ></el-input>
      <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <div class="history-division" v-show="showHistory" @mouseenter="mouseOverHistory=true" @mouseleave="mouseOverHistory=false">
        <div class="history-head">
          <span>搜索历史</span>
          <div class="del-btn" @click="deleteHistory">
            <img src="~@/assets/dashboard/del.png" width="14" height="14" alt="">
          </div>
        </div>
        <div class="history-content">
          <template v-if="historyList.length">
            <div
              class="history-item"
              v-for="(item,index) in historyList"
              @click="chooseHistory(item)"
            >{{ item }}</div>
          </template>
          <span class="history-empty" v-else>暂无搜索历史</span>
        </div>
      </div>
    </div>
<!--    管理搜索选项-->
    <search-type-manage
      :dialog-visible="manageDialog.show"
      :options="manageDialog.options"
      @close="closeManage"
      @save="saveManage"
    ></search-type-manage>
  </div>
</template>

<script>
import { mapState } from "vuex"
import popupSelector from './popupSelector'
import searchTypeManage from './dialogs/searchTypeManage'
export default {
  components: {
    popupSelector,
    searchTypeManage,
  },
  name: "",
  props: {
    // searchTypes: {
    //   type: Array,
    //   default: []
    // },
    // searchText: {
    //   type: String,
    //   default: ''
    // },
  },
  data() {
    return {
      searchTypesShowIndex: 0,
      moreVisible: false,
      erd: null,
      manageDialog: {
        show: false,
        options: [],
      },
      showHistory: false,
      historyList: ['资产','盛辉物流','车管项目进度','1300009','张三','王五','华南营业部','广播','对账单','9月','盛辉'],
      mouseOverHistory: false,
      mouseOverInput: false,
    }
  },
  computed: {
    ...mapState(["myInfo", "icReadDriveOpen"]),
    searchTypes: {
      get(){
        return this.$store.state.dashboard.searchTypes
      },
      set(val){
        this.$store.commit('setDashboardSearchTypes', val)
      }
    },
    searchText:{
      get(){
        return this.$store.state.dashboard.searchText
      },
      set(val){
        this.$store.commit('setDashboardSearchText', val)
      }
    },
    showSearchTypes() {
      return this.searchTypesShowIndex>=this.searchTypes.length-1?this.searchTypes:this.searchTypes.slice(0,this.searchTypesShowIndex)
    },
    moreActiveIndex() {
      let nowIndex = this.searchTypes.findIndex((item)=>{
        return item.active
      })
      return nowIndex < this.searchTypesShowIndex ? -1 : (nowIndex - this.searchTypesShowIndex)
    },
  },
  mounted() {
    this.getTypesRowWidth()
    window.addEventListener('click',this.hideHistory)
  },
  beforeDestroy () {
    if(this.erd){
      this.erd.uninstall(this.$refs['typesRow'])
    }
    window.removeEventListener('click',this.hideHistory)
  },
  methods: {
    //监听宽度
    getTypesRowWidth() {
      let elementResizeDetectorMaker = require("element-resize-detector")
      this.erd = elementResizeDetectorMaker()
      this.$nextTick(()=>{
        this.erd.listenTo(
          this.$refs['typesRow'], (element)=>{
            console.log(element.clientWidth,'宽度')
            let nowWidth = 0
            let i = 0
            for(i=0;i<this.searchTypes.length;i++) {
              nowWidth += (24 + 14*this.searchTypes[i].text.length)
              if(nowWidth >= element.clientWidth-140){
                break
              }
            }
            this.searchTypesShowIndex = i
            this.moreVisible = false
          }
        )
      })
    },
    //改变搜索类型
    changeSearchType(item,index){
      this.searchTypes.forEach((it,i)=>{
        if (index===i) {
          it.active = true
        }
        else {
          it.active = false
        }
      })
    },
    //选择更多搜索类型
    chooseMoreType(index) {
      let item = this.searchTypes.slice(this.searchTypesShowIndex)[index]
      this.searchTypes.forEach((it,i)=>{
        if (it.text === item.text) {
          it.active = true
        }
        else {
          it.active = false
        }
      })
      this.moreVisible = false
    },
    //管理选项弹窗
    openManage() {
      this.manageDialog = {
        show: true,
        options: this.$utilJs.deepClone(this.searchTypes)
      }
    },
    closeManage() {
      this.manageDialog.show = false
    },
    saveManage(list) {
      this.searchTypes = list
      this.manageDialog.show = false
    },
    //点击搜索历史
    chooseHistory(text) {
      this.searchText = text
      this.showHistory = false
      //this.$store.commit('setDashboardSearchText', text)
    },
    //删除搜索历史
    deleteHistory() {
      this.historyList = []
      this.showHistory = false
    },
    //隐藏搜索历史
    hideHistory(){
      if(!this.mouseOverHistory && !this.mouseOverInput){
        this.showHistory = false
      }
    },
    //点击搜索
    handleSearch() {
      let obj = this.searchTypes.find((item)=>{
        return item.active
      })
      let type = obj ? obj.text : '综合'
      this.$store.commit('setSearchResultSearchText',this.searchText)
      this.$store.commit('setSearchResultSearchType',type)
      window.$addTab('搜索结果')
    },
  },
  watch: {
    // "reqLoading.state"(v) {
    //   console.log(v, 45111111);
    // },
  },
};
</script>
<style lang="less" scoped>
@textColor: #1f1e1d;
@ntalkColor: #e67529;
.search-header {
  width: 100%;
  margin: 32px 0 24px;
  .types-row {
    //min-width: 590px;
    max-width: calc(100% - 194px);
    display: flex;
    align-items: center;
    .search-types {
      //width: calc(100% - 140px);
      width: 100%;
      display: flex;
      flex-wrap: nowrap;
      .item {
        height: 22px;
        line-height: 22px;
        padding: 0 8px;
        font-size: 14px;
        color: @textColor;
        cursor: pointer;
        border-radius: 6px;
        margin-right: 8px;
        flex-shrink: 0;
        position: relative;
        &:hover {
          background-color: rgba(0,0,0,0.06);
        }
        &.active {
          background-color: @ntalkColor;
          color: #fff;
          &:after {
            content: '';
            position: absolute;
            right: 50%;
            bottom: 0;
            transform: rotate(45deg) translateX(6px);
            width: 8.5px;
            height: 8.5px;
            background-color: @ntalkColor;
            z-index: 100;
          }
        }
      }
      .more-item {
        height: 22px;
        line-height: 22px;
        padding: 0 8px;
        font-size: 14px;
        color: @textColor;
        cursor: pointer;
        border-radius: 6px;
        margin-right: 8px;
        display: flex;
        align-items: center;
        position: relative;
        flex-shrink: 0;
        img {
          margin-left: 6px;
        }
        &:hover {
          background-color: rgba(0,0,0,0.06);
        }
      }
      .item-divider {
        width: 2px;
        height: 22px;
        background-color: #ccc;
        flex-shrink: 0;
      }
      .options-item {
        height: 22px;
        line-height: 22px;
        padding: 0 8px;
        font-size: 14px;
        color: @textColor;
        cursor: pointer;
        border-radius: 6px;
        margin-left: 8px;
        display: flex;
        align-items: center;
        flex-shrink: 0;
        img {
          margin-right: 6px;
        }
        &:hover {
          background-color: rgba(0,0,0,0.06);
        }
      }
    }
  }
  .search-row {
    width: calc(100% - 207px);
    height: 40px;
    display: flex;
    align-items: center;
    margin-top: 12px;
    position: relative;
    .history-division {
      position: absolute;
      top: 44px;
      left: 0;
      z-index: 100000;
      width: 100%;
      min-height: 154px;
      border-radius: 6px;
      background-color: #fff;
      box-shadow: 0px 6px 16px 0px rgba(0, 0, 0, 0.08),0px 3px 6px -4px rgba(0, 0, 0, 0.12),0px 9px 28px 8px rgba(0, 0, 0, 0.05);
      box-sizing: border-box;
      padding: 16px 8px;
      .history-head {
        width: 100%;
        display: flex;
        justify-content: space-between;
        height: 22px;
        span {
          color: @textColor;
          font-size: 14px;
        }
        .del-btn {
          width: 22px;
          height: 22px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
          border-radius: 4px;
          &:hover {
            background-color: rgba(0,0,0,0.06);
          }
        }
      }
      .history-content {
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        margin-top: 12px;
        .history-item {
          margin-right: 8px;
          margin-bottom: 8px;
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 0 8px;
          border-radius: 12px;
          height: 24px;
          font-size: 12px;
          color: rgba(0,0,0,0.66);
          background-color: #efeff5;
          cursor: pointer;
          &:hover {
            color: #e67529;
            background-color: #fff0e5;
          }
        }
        .history-empty {
          color: rgba(0,0,0,0.44);
        }
      }
    }
    /deep/ .el-input {
      width: calc(100% - 88px);
      > input {
        border-top-right-radius: 0;
        border-bottom-right-radius: 0;
        border-right: 0;
        &:focus {
          border-color: #DCDFE6;
        }
      }
    }
    /deep/ .el-button {
      border-top-left-radius: 0;
      border-bottom-left-radius: 0;
    }
  }
}
</style>
